package com.yaowk.utils.generator;

import com.yaowk.common.api.BaseModel;
import com.yaowk.common.db.GeneratorNote;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@EqualsAndHashCode(callSuper = false)
public class ModelBaseGenerator extends AbstractGenerator {

    private String classTemplate = "@Getter%n@Setter%n@ToString%npublic abstract class %s extends BaseModel<%s> {%n%n";
    private String fieldTemplate = "\t@ApiModelProperty(\"%s\")%n\tprivate %s %s;%n";

    public ModelBaseGenerator() {
        setSuffix("Base");
    }

    @Override
    public void generator() {
        List<Class> entities = getEntities();
        String packageName = getPackageName();
        if (StringUtils.isEmpty(packageName)) {
            return;
        }
        String path = GeneratorUtils.getPath(packageName, getBasePath());
        for (Class clazz : entities) {
            // 构建类名
            String className = buildClassName(clazz);
            String newClazz = buildClassType(clazz);
            File file = new File(path + "/" + className + ".java");
            if (file.exists()) {
                file.delete();
            }
            StringBuilder stringBuilder = new StringBuilder();

            // 设置包名
            appendPackage(stringBuilder);

            // 组织引用类
            Set<String> imports = new HashSet<>();
            imports.add(BaseModel.class.getTypeName());
            imports.add(Getter.class.getTypeName());
            imports.add(Setter.class.getTypeName());
            imports.add(ToString.class.getTypeName());
            imports.add(ApiModelProperty.class.getTypeName());
            String[] pk = GeneratorUtils.getGenericSuper(clazz);
            imports.add(pk[1]);

            // 设置属性
            Field[] fields = clazz.getDeclaredFields();

            StringBuilder fieldBuilder = new StringBuilder();

//            if (GeneratorUtils.needId(clazz)) {
//                fieldBuilder.append("\t@ApiModelProperty(\"id\")\n\tprivate ")
//                        .append(pk[0])
//                        .append(" id;\n");
//            }
            for (Field field : fields) {
                // 自定义注解，用来描述字段，也用来表示该参数需要生成model属性
                GeneratorNote generatorNote = field.getAnnotation(GeneratorNote.class);
                if (ObjectUtils.isEmpty(generatorNote)) {
                    continue;
                }

//                ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
                String[] fieldType = GeneratorUtils.parseClassType(field.getType().getTypeName());
                String fieldName = field.getName();
//                if (!ObjectUtils.isEmpty(manyToOne)) {
//                    fieldType = GeneratorUtils.getGenericSuper(field.getType());
//                    fieldName = fieldName + "Id";
//                }

                imports.add(fieldType[1]);
                fieldBuilder.append(String.format(fieldTemplate, generatorNote.value(), fieldType[0], fieldName));
            }

            appendImport(stringBuilder, imports);
            // 设置类
            stringBuilder.append(String.format(classTemplate, className, pk[0]));
            stringBuilder.append(fieldBuilder);
            stringBuilder.append("\n}");

            // 执行生成
            GeneratorUtils.write(file, stringBuilder);
            System.out.println(newClazz);
        }
    }
}
